﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GBMS.APP_UI.Common;
using Telerik.Web.UI;

namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class BGM_Strategy_Data_List : System.Web.UI.Page
    {
        private IStrategyDataBLL stratCtrl = new StrategyDataBLL();
        private CommandControl cmdFac = new CommandControl();
        private RadComboBox StrategyTypeCombo;
        private Main main = new Main();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadYearCombo();
                cbType.LoadStrategyTypeComboBox();
                ChangeMode(Main.EditMode.View);
            }
            
                LoadDataGrid();
            Page.DataBind();
        }

        private void LoadDataGrid()
        {
            LoadDataGrid(0);
        }

        private void LoadDataGrid(int seq)
        {
            Grid.DataSource = stratCtrl.Search(seq);
            Grid.DataBind();
        }

        private void ChangeMode(Main.EditMode mode)
        {
            switch (mode)
            {
                case Main.EditMode.View:

                    lbtShowGrid.Visible = true;
                    lbtAddNew.Visible = true;
                    lbtEdit.Visible = false;
                    lbtDelete.Visible = false;
                    lbtSave.Visible = false;
                    lbtCancel.Visible = false;
                    panelGrid.Visible = true;
                    panelList.Visible = false;
                    
                    break;

                case Main.EditMode.AddNew:

                    lbtShowGrid.Visible = true;
                    lbtAddNew.Visible = true;
                    lbtEdit.Visible = false;
                    lbtDelete.Visible = false;
                    lbtSave.Visible = true;
                    lbtCancel.Visible = true;
                    panelGrid.Visible = false;
                    panelList.Visible = true;

                    break;

                case Main.EditMode.Edit:

                    lbtShowGrid.Visible = true;
                    lbtAddNew.Visible = false;
                    lbtEdit.Visible = false;
                    lbtDelete.Visible = true;
                    lbtSave.Visible = true;
                    lbtCancel.Visible = true;
                    panelGrid.Visible = false;
                    panelList.Visible = true;

                    break;
            }
        }

        private void LoadYearCombo()
        {
            int CurrYear = SystemInfo._CurrentYear;
            int realCurrYear = Convert.ToInt32(GetCurrentYear());
            int startYear = 2551 < CurrYear && 2551 < realCurrYear
                                    ? 2551
                                    : (CurrYear < realCurrYear ? CurrYear : realCurrYear);

            int endYear = (CurrYear > realCurrYear ? CurrYear : realCurrYear) + 15;
            for (int i = startYear; i < endYear; i++)
            {
                ysCombo.Items.Add(new RadComboBoxItem(i.ToString(), i.ToString()));
                yeCombo.Items.Add(new RadComboBoxItem(i.ToString(), i.ToString()));
            }
        }

        protected void Grid_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "TargetClick")
            {
                EditorStratSeq = Convert.ToInt32(((Label)e.Item.FindControl("targetSequence")).Text);
                BindDataEditor();
                ChangeMode(Main.EditMode.Edit);
            }
            else if (e.CommandName == "Filter" || e.CommandName == "Sort" || e.CommandName == "Page")
            {
                LoadDataGrid();
                ChangeMode(Main.EditMode.View);
            }
        }

        private void BindDataEditor()
        {
            IList<StrategyDataInfo> stratList = stratCtrl.Search(EditorStratSeq ?? 0,
                                                                 "",
                                                                 "",
                                                                 "",
                                                                 0);
            txtCode.Text = stratList[0].Code;
            txtName.Text = stratList[0].Name;
            txtDescr.Text = stratList[0].Description;
            txtGFMISCode.Text = stratList[0].Gfmis_code;
            ((RadComboBox)cbType.FindControl("cbType")).SelectedValue = stratList[0].StrategyType.Sequence.ToString();
            ysCombo.SelectedValue = stratList[0].YearStart;
            yeCombo.SelectedValue = stratList[0].YearEnd;
        }

        #region Private Field

        private string GetCurrentYear()
        {
            return DateTime.Now.ToString("yyy", CultureInfo.GetCultureInfo("th-TH"));
        }

        public enum ViewMode
        {
            ShowAll,
            View,
            Edit
        }

        private ViewMode _currentview = ViewMode.ShowAll;

        #endregion

        #region Public Data field

        public bool GridVisible
        {
            get { return Grid.Visible; }
            set { Grid.Visible = value; }
        }

        public bool EditorVisible
        {
            get { return panelList.Visible; }
            set { panelList.Visible = value; }
        }

        public int? EditorStratSeq
        {
            get { return String.IsNullOrEmpty(hdSeq.Text) ? (int?)null : Convert.ToInt32(hdSeq.Text); }
            set { hdSeq.Text = value == null ? null : value.ToString(); }
        }

        public string EditorStratCode
        {
            get { return txtCode.Text; }
            set { txtCode.Text = value; }
        }

        public string EditorGFMISCode
        {
            get { return txtGFMISCode.Text; }
            set { txtGFMISCode.Text = value; }
        }

        public string EditorStratName
        {
            get { return txtName.Text; }
            set { txtName.Text = value; }
        }

        public string EditorDescription
        {
            get { return txtDescr.Text; }
            set { txtDescr.Text = value; }
        }

        public string EditorStratType
        {
            get { return ((RadComboBox)cbType.FindControl("cbType")).SelectedValue; }
            set { ((RadComboBox)cbType.FindControl("cbType")).SelectedValue = value; }
        }

        public string EditorYearStart
        {
            get { return ysCombo.SelectedValue; }
            set { ysCombo.SelectedValue = value; }
        }

        public string EditorYearEnd
        {
            get { return yeCombo.SelectedValue; }
            set { yeCombo.SelectedValue = value; }
        }

        public ViewMode CurrentView
        {
            get { return _currentview; }
            set
            {
                _currentview = value;
            }
        }

        #endregion end public

        #region Method

        public void ChangeMethod(ViewMode pViewMode)
        {
            CurrentView = pViewMode;
            // UpdateVisibleDataFieldAndGrid();
        }

        //public void UpdateVisibleDataFieldAndGrid()
        //{
        //    GridVisible = CurrentView == ViewMode.ShowAll;
        //    EditorVisible = CurrentView != ViewMode.ShowAll;
        //}

        #endregion

        #region Event

        protected void CommandFire(object sender, CommandEventArgs e)
        {
            ViewMode newViewMode;
            switch (e.CommandName)
            {
                case "ShowGrid":
                    ChangeMode(Main.EditMode.View);
                    newViewMode = ViewMode.ShowAll;
                    break;
                case "AddNew": 
                    ChangeMode(Main.EditMode.AddNew);
                    ClearFormWithSeq();
                    newViewMode = ViewMode.Edit;
                    break;
                case "Edit":
                    ChangeMode(Main.EditMode.Edit);
                    BindDataEditor();
                    newViewMode = ViewMode.Edit;
                    break;
                case "Cancel":
                    LoadDataGrid();
                    ChangeMode(Main.EditMode.View);
                    BindDataEditor();
                    newViewMode = ViewMode.ShowAll;
                    break;
                case "Save":

                    newViewMode = InsertOrUpdate() ? ViewMode.ShowAll : ViewMode.Edit;
                    break;
                case "Delete":
                    Delete();
                    ChangeMode(Main.EditMode.View);
                    newViewMode = ViewMode.ShowAll;
                    break;
                default:
                    ChangeMode(Main.EditMode.View);
                    newViewMode = ViewMode.ShowAll;
                    break;
            }
            CurrentView = newViewMode;
            //ChangeMethod(newViewMode);
        }
        
        private bool InsertOrUpdate()
        {
            bool isFormValid = CheckFormEditorValid();
            string addNewOrUpdate = String.IsNullOrEmpty(hdSeq.Text) ? "AddNew" : "Update";

            if (!isFormValid) return false;

            if (addNewOrUpdate == "AddNew")
            {
                ChangeMode(Main.EditMode.AddNew);

                stratCtrl.Insert(txtCode.Text,
                                 txtName.Text,
                                 txtDescr.Text,
                                 Convert.ToInt32(((RadComboBox)cbType.FindControl("cbType")).SelectedValue),
                                 ysCombo.SelectedValue,
                                 yeCombo.SelectedValue,
                                 txtGFMISCode.Text);
             }
            else
            {
                ChangeMode(Main.EditMode.Edit);

                stratCtrl.Update(Convert.ToInt32(hdSeq.Text),
                                 txtCode.Text,
                                 txtName.Text,
                                 txtDescr.Text,
                                 Convert.ToInt32(((RadComboBox)cbType.FindControl("cbType")).SelectedValue),
                                 ysCombo.SelectedValue,
                                 yeCombo.SelectedValue,
                                 txtGFMISCode.Text);
              }

            LoadDataGrid();
            ChangeMode(Main.EditMode.View);
            return true;
        }

        private void Delete()
        {
           // if (panelGrid.Visible == true)
           // {

                  // stratCtrl.Delete(Convert.ToInt32(item.OwnerTableView.DataKeyValues[item.ItemIndex]["Sequence"]));
                
           // }
            //else
            //{
                stratCtrl.Delete(Convert.ToInt32(hdSeq.Text));
            //}

            LoadDataGrid();
            ChangeMode(Main.EditMode.View);
        }

        private bool CheckFormEditorValid()
        {
            bool formValid = true;

            if (Convert.ToInt32(ysCombo.SelectedValue) > Convert.ToInt32(yeCombo.SelectedValue))
            {
                main.MessageAlert(Page, "ปีเริ่มต้นต้องไม่มากกว่าปีสิ้นสุด");
                formValid = false;
            }

            //if (stratCtrl.CheckDuplicateCode(txtCode.Text, hdSeq.Text))
            //{
            //    main.MessageAlert(Page, "รหัสซ้ำ");
            //    formValid = false;
            //}

            if (!main.IsDouble(txtCode.Text))
            {
                main.MessageAlert(Page, "กรอกเป็นตัวเลขเท่านั้น");
                formValid = false;
            }

            if (!main.IsInt32(((RadComboBox)cbType.FindControl("cbType")).SelectedValue))
            {
                main.MessageAlert(Page, "กรุณาเลือกประเภทยุทธศาสตร์");
                formValid = false;
            }
            return formValid;
        }

        private void ClearFormWithSeq()
        {
            EditorStratSeq = null;
            ClearForm();
        }

        private void ClearForm()
        {
            EditorStratCode = String.Empty;
            EditorGFMISCode = String.Empty;
            EditorStratName = String.Empty;
            EditorDescription = String.Empty;
            EditorStratType = String.Empty;
            EditorYearStart = GetCurrentYear();
            EditorYearEnd = GetCurrentYear();
        }

        #endregion
    }
}
